﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent.Bestiary;
using Terraria.ID;
using Terraria.ModLoader;

namespace MasterMasterMode.Content.NPCs
{
    public class GiantBunny : ModNPC
    {
        private bool hasPlayedReloadSound = false;
        private int reload_gun;
        public override void SetDefaults()
        {
            NPC.lifeMax = 250;
            NPC.damage = 36;
            NPC.defense = 20;
            NPC.knockBackResist = 0f;
            NPC.width = 40;
            NPC.height = 40;
            AnimationType = 46;
            NPC.aiStyle = 3;
            AIType = 73;
            NPC.npcSlots = 0.8f;
            NPC.HitSound = SoundID.NPCHit1;
            NPC.DeathSound = SoundID.NPCDeath1;
            NPC.value = Item.buyPrice(0, 1, 50, 0);
        }
        public override void SetBestiary(BestiaryDatabase database, BestiaryEntry bestiaryEntry)
        {
            bestiaryEntry.Info.AddRange([
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Times.DayTime,
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Biomes.Surface,
                new FlavorTextBestiaryInfoElement("Mods.MasterMasterMode.Bestiary.GiantBunny"),
            ]);
        }
        public override void SetStaticDefaults()
        {
            Main.npcFrameCount[NPC.type] = 7;
        }

        public override void ApplyDifficultyAndPlayerScaling(int numPlayers, float balance, float bossAdjustment)
        {
            NPC.lifeMax = (int)(NPC.lifeMax * 1.2f);
            NPC.damage = (int)(NPC.damage * 1.2f);
        }
        public override void HitEffect(NPC.HitInfo hit)
        {
            if (NPC.life <= 0)
            {
                for (int i = 0; i < 20; i++)
                {
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Blood, 2.5f * hit.HitDirection, -2.5f, 0, default, 2f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Blood, 2.5f * hit.HitDirection, -2.5f, 0, default, 2f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Blood, 2.5f * hit.HitDirection, -2.5f, 0, default, 2f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Blood, 2.5f * hit.HitDirection, -2.5f, 0, default, 2f);
                }
                Gore.NewGore(NPC.GetSource_Death(), NPC.position, NPC.velocity, Mod.Find<ModGore>("GiantFoot").Type, 1f);
                Gore.NewGore(NPC.GetSource_Death(), NPC.position, NPC.velocity, Mod.Find<ModGore>("GiantLeg").Type, 1f);
                Gore.NewGore(NPC.GetSource_Death(), NPC.position, NPC.velocity, Mod.Find<ModGore>("GiantBody").Type, 1f);
                Gore.NewGore(NPC.GetSource_Death(), NPC.position, NPC.velocity, Mod.Find<ModGore>("GiantHead").Type, 1f);
            }
            if (NPC.life >= 0)
            {
                for (int j = 0; j < 20; j++)
                {
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Blood, 2.5f * hit.HitDirection, -2.5f, 0, default, 1f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Blood, 2.5f * hit.HitDirection, -2.5f, 0, default, 1f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Blood, 2.5f * hit.HitDirection, -2.5f, 0, default, 1f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Blood, 2.5f * hit.HitDirection, -2.5f, 0, default, 1f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Blood, 2.5f * hit.HitDirection, -2.5f, 0, default, 1f);
                }
            }
        }
        public override void AI()
        {
            reload_gun++;
            Player player = Main.player[NPC.target];
            if (reload_gun == 60 && !hasPlayedReloadSound)
            {
                SoundStyle Sound = new($"{nameof(MasterMasterMode)}/Assets/Sounds/Reload");
                SoundEngine.PlaySound(Sound with { Pitch = 0.3f, Volume = 0.8f }, NPC.Center);
                hasPlayedReloadSound = true;
            }

            if (reload_gun == 90)
            {
                Vector2 vector;
                vector = new(NPC.position.X + NPC.width / 2, NPC.position.Y + NPC.height / 2);
                Gore.NewGore(NPC.GetSource_FromAI(), NPC.position, NPC.velocity * 3f * -1f, Mod.Find<ModGore>("Shell").Type, 0.77f);
                int num = Projectile.NewProjectile(NPC.GetSource_FromAI(), vector.X, vector.Y, 18 * NPC.spriteDirection, 0f, 14, 30, 0f, 0, 0f, 0f);
                Main.projectile[num].friendly = false;
                Main.projectile[num].hostile = true;
                reload_gun = 0;
                hasPlayedReloadSound = false;
                SoundStyle Sound = new($"{nameof(MasterMasterMode)}/Assets/Sounds/Shoot");
                SoundEngine.PlaySound(Sound with { Pitch = -0.2f, Volume = 0.9f }, NPC.Center);
            }
            if (player.dead)
            {
                hasPlayedReloadSound = false;
                reload_gun = 0;
            }
        }
    }
}
